/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("productinformationmodel",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.demo.IServices;

namespace VOL.demo.Controllers
{
    public partial class productinformationmodelController
    {
        private readonly IproductinformationmodelService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public productinformationmodelController(
            IproductinformationmodelService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 获取货品类型级联数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetProductTypeCascade")]
        public IActionResult GetProductTypeCascade()
        {
            try
            {
                var result = _service.GetProductTypeCascade();
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 生成条码
        /// </summary>
        /// <param name="goodsId">货品ID</param>
        /// <param name="unitId">单位ID</param>
        /// <returns></returns>
        [HttpGet("GenerateBarcode")]
        public IActionResult GenerateBarcode(long goodsId, long unitId)
        {
            try
            {
                var barcode = _service.GenerateBarcode(goodsId, unitId);
                return Json(new { success = true, data = barcode });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 货品信息新增（包含单位管理、库存信息、图片上传）
        /// </summary>
        /// <param name="request">新增请求</param>
        /// <returns></returns>
        [HttpPost("AddProductWithDetails")]
        public IActionResult AddProductWithDetails([FromBody] AddProductRequest request)
        {
            try
            {
                var result = _service.AddProductWithDetails(
                    request.ProductInfo,
                    request.UnitRelations,
                    request.InventoryList,
                    request.ImageList
                );
                
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message, data = result.Data });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }
    }

    /// <summary>
    /// 新增货品请求模型
    /// </summary>
    public class AddProductRequest
    {
        public productinformationmodel ProductInfo { get; set; }
        public List<GoodsUnitRelationModel> UnitRelations { get; set; }
        public List<InventoryModel> InventoryList { get; set; }
        public List<GoodsImageModel> ImageList { get; set; }
    }
}
